﻿using PlayerRoles;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;


namespace Event.Rule
{
    public class BindLimtEvent
    {
        [PluginEvent(ServerEventType.PlayerDying)]
        public void OnPlayerDying(PlayerDyingEvent e)
        {
            if (!e.Player.IsDisarmed) return;

            if (e.Attacker.Team == Team.Dead) return;
            if (e.Attacker.Team == Team.OtherAlive) return;
            if (e.Attacker.Team == Team.SCPs) return;

            if (e.Attacker == e.Player.DisarmedBy)
            {
                e.Player.SendBroadcast(GetResult(), 15, Broadcast.BroadcastFlags.Normal, true);
                return;
            }

            switch (e.Player.Role.GetTeam())
            {
                case Team.ClassD:
                    e.Player.SendBroadcast(GetDescrition(), 15, Broadcast.BroadcastFlags.Normal, true);
                    e.Player.SetRole(RoleTypeId.ChaosMarauder);
                    break;
                case Team.Scientists:
                    e.Player.SendBroadcast(GetDescrition(), 15, Broadcast.BroadcastFlags.Normal, true);
                    e.Player.SetRole(RoleTypeId.NtfSergeant);
                    break;
                default:
                    break;
            }

            this.PunishAttacker(e.Attacker);
        }


        private string GetDescrition()
        {
            string description = "你因为死于来自非负责人员的绑伤，所以将直接作为友军武装阵营增援设施";
            MessageStringUtils.GenerateAdminMessage(description, out string outout);
            return outout;
        }

        private string GetResult()
        {
            string description = 
                "你的行为被武装人员怀疑，因此受到被捆绑者的处决，这并不违规服务器规则" +
                "\n最终解释权在负责捆绑押运人员上";
            MessageStringUtils.GenerateAdminMessage(description, out string outout);
            return outout;
        }

        private void PunishAttacker(Player player)
        {
            player.SetRole(RoleTypeId.Spectator, RoleChangeReason.None);
            string message =
                $"{player.Nickname} | " +
                $"违规<color=#FF0000>击毙</color>捆绑玩家 | 请查阅 Server Info 遵守规则 " +
                $"\n<color=#FF0000>[惩罚]</color> 立即进入观察者模式";

            MessageStringUtils.GenerateAdminMessage(message, out string output, 20);
            Server.SendBroadcast(output, 15, Broadcast.BroadcastFlags.Normal, shouldClearPrevious: true);
        }
    }
}
